package com.wgts.wgts.mvp.view.activity.shopCar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wgts.wgts.ActivityStack;
import com.wgts.wgts.R;
import com.wgts.wgts.control.dialog.ShopDeleteDialogFragment;
import com.wgts.wgts.control.statusBar.IsplayHelper;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.mvp.presenter.IShoppingCarPersenter;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.netmodle.resp.ShopCarListEntity;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/9/616:23
 *
 */public class ShoppingCarRecycleAdapter extends RecyclerView.Adapter<ShoppingCarRecycleAdapter.ViewHold> {
    private Context context;
    private List<ShopCarListEntity.product> list;
    private IShoppingCarPersenter persenter;
    private FragmentManager fragmentManager;
    private int number;

    public ShoppingCarRecycleAdapter(Context context, List<ShopCarListEntity.product> list, IShoppingCarPersenter persenter, FragmentManager fragmentManager) {
        this.context = context;
        this.list = list;
        this.persenter = persenter;
        this.fragmentManager = fragmentManager;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.shooping_car_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHold viewHold, final int i) {
        MoHuGlide.getInstance().glideImg(context, list.get(i).productSmallImage, viewHold.shoppingCarItemImage);
        viewHold.shoppingCarTypeShow.setText(list.get(i).productStandard);
        if (list.get(i).isCheck) {
            list.get(i).isCheck = true;
            viewHold.shoppingCheckImage.setSelected(true);
        } else {
            list.get(i).isCheck = false;
            viewHold.shoppingCheckImage.setSelected(false);
        }
        viewHold.shoppingCarTitle.setText(list.get(i).productName);
        viewHold.shoppingCarPrice.setText("￥" + list.get(i).productPrice);
        viewHold.shoppingCarChangeNumber.setText(list.get(i).productCount);
        if (Integer.parseInt(list.get(i).productCount) <= 1){
            viewHold.shoppingSubtract.setClickable(false);
            viewHold.shoppingSubtract.setImageResource(R.mipmap.no_subtract);
        }else {
            viewHold.shoppingSubtract.setClickable(true);
            viewHold.shoppingSubtract.setImageResource(R.mipmap.nomarl_subtract);
        }
        /*
         * 列表选中按钮事件
         * */
        viewHold.shoppingCarCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(i).isCheck) {
                    persenter.ShopCheck(list.get(i).Pposition, i, false);
                } else {
                    persenter.ShopCheck(list.get(i).Pposition, i, true);
                }
                persenter.chooseNumber();
                persenter.totalPrice();
                persenter.isAllCheck();
            }
        });
        /*
         * 商品数目加减
         * */
        viewHold.shoppingSubtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    number = Integer.parseInt(list.get(i).productCount);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                if (number <= 1) {
                    MoHuToast.showToast("商品数量不能少于1");
                    viewHold.shoppingSubtract.setImageResource(R.mipmap.no_subtract);
                } else {
                    viewHold.shoppingSubtract.setImageResource(R.mipmap.nomarl_subtract);
                    persenter.addOrSubtrctShopping(list.get(i).Pposition, i, false);
                    //persenter.totalPrice();
                    persenter.chooseNumber();
                }
            }
        });
        viewHold.shoppingAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                persenter.addOrSubtrctShopping(list.get(i).Pposition, i, true);
                persenter.totalPrice();
                persenter.chooseNumber();
            }
        });
        /*
         * 删除商品事件
         * */
        viewHold.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final ShopDeleteDialogFragment deleteDialogFragment = ShopDeleteDialogFragment.getShopDeleteDialogFragment("删除商品");
                deleteDialogFragment.show(fragmentManager, "deleteShop");
                deleteDialogFragment.setOnClickListener(new ShopDeleteDialogFragment.OnClickListener() {
                    @Override
                    public void delete() {
                        persenter.deleteShopping(list.get(i).Pposition, i);
                        persenter.totalPrice();
                        persenter.chooseNumber();
                        persenter.isAllCheck();
                        deleteDialogFragment.dismiss();
                    }
                });
                return true;
            }
        });
        /*
         * 商品详情页入口
         * */
       viewHold.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                intent.putExtra("id", list.get(i).productId);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.shopping_check_image)
        ImageView shoppingCheckImage;
        @BindView(R.id.shopping_car_check)
        LinearLayout shoppingCarCheck;
        @BindView(R.id.shopping_car_item_image)
        ImageView shoppingCarItemImage;
        @BindView(R.id.shopping_car_title)
        TextView shoppingCarTitle;
        @BindView(R.id.shopping_car_type_show)
        TextView shoppingCarTypeShow;
        @BindView(R.id.shopping_car_price)
        TextView shoppingCarPrice;
        @BindView(R.id.shopping_subtract)
        ImageView shoppingSubtract;
        @BindView(R.id.shopping_car_change_number)
        TextView shoppingCarChangeNumber;
        @BindView(R.id.shopping_add)
        ImageView shoppingAdd;
        @BindView(R.id.shopping_number_ly)
        LinearLayout shoppingNumberLy;
        @BindView(R.id.shopping_car_details)
        LinearLayout shoppingCarDetails;
        @BindView(R.id.shopping_swipe_list)
        LinearLayout shoppingSwipeList;
        private View itemView;
        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    /**
     * 设置某个View的margin
     *
     * @param view   需要设置的view
     * @param isDp   需要设置的数值是否为DP
     * @param left   左边距
     * @param right  右边距
     * @param top    上边距
     * @param bottom 下边距
     * @return
     */
    public static ViewGroup.LayoutParams setViewMargin(View view, boolean isDp, int left, int right, int top, int bottom) {
        if (view == null) {
            return null;
        }

        int leftPx = left;
        int rightPx = right;
        int topPx = top;
        int bottomPx = bottom;
        ViewGroup.LayoutParams params = view.getLayoutParams();
        ViewGroup.MarginLayoutParams marginParams = null;
        //获取view的margin设置参数
        if (params instanceof ViewGroup.MarginLayoutParams) {
            marginParams = (ViewGroup.MarginLayoutParams) params;
        } else {
            //不存在时创建一个新的参数
            marginParams = new ViewGroup.MarginLayoutParams(params);
        }

        //根据DP与PX转换计算值
        if (isDp) {
            leftPx = IsplayHelper.dpToPx(left);
            rightPx = IsplayHelper.dpToPx(right);
            topPx = IsplayHelper.dpToPx(top);
            bottomPx = IsplayHelper.dpToPx(bottom);
        }
        //设置margin
        marginParams.setMargins(leftPx, topPx, rightPx, bottomPx);
        view.setLayoutParams(marginParams);
        return marginParams;
    }


}
